Golang : Get current time
Problem :
Need to get current time stamp in Go and display in string.
Solution :
Use the time.Local() and time.Format() functions.
getcurrenttime.go
package main
import (
"fmt"
"os"
"time"
)
func main() {
// get current timestamp
currenttime := time.Now().Local()
fmt.Println("Current time : ", currenttime.Format("2006-01-02 15:04:05 +0800"))
}
Execute the code
>go run getcurrenttime.go
and the output will be :
Current time : 2014-06-30 14:23:38 +0800
Take note that +0800
is the timezone in my current location. You can change it to suit your timezone.
Reference :
See also : Golang : Get file last modified date and time
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.9k Golang : How to get quoted string into another string?
+21.4k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+11.7k Golang : Convert a rune to unicode style string \u
+19.7k Golang : Determine if directory is empty with os.File.Readdir() function
+15.4k Golang : Get current time from the Internet time server(ntp) example
+32.5k Golang : How to check if a date is within certain range?
+5.1k Unix/Linux/MacOSx : How to remove an environment variable ?
+10.8k Golang : Fix - does not implement sort.Interface (missing Len method)
+12k Golang : How to check if a string starts or ends with certain characters or words?
+6.5k Golang : Calculate pivot points for a cross
+15.6k Golang : Read a file line by line
+8.2k Golang : Add text to image and get OpenCV's X, Y co-ordinates example